<?php
/*
fileHashDB
Copyright (c) 2010 The Hursk Group, LLC. All rights reserved.

www.hursk.com

hurskgroup@hursk.com

This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

*/

// Put the files below in the stage directory
// NSRLFile.txt NSRLMfg.txt NSRLOS.txt NSRLProd.txt
$stage = '../data/stage/';
$data_store = '../data/nsrl_dat/2_30/'; // location of the database

/*
NSRLFile.txt = "SHA-1","MD5","CRC32","FileName","FileSize","ProductCode","OpSystemCode","SpecialCode"
NSRLMfg.txt = "MfgCode","MfgName"
NSRLOS.txt = "OpSystemCode","OpSystemName","OpSystemVersion","MfgCode"
NSRLProd.txt = "ProductCode","ProductName","ProductVersion","OpSystemCode","MfgCode","Language","ApplicationType"

xml
<file_hash>
</file_hash>
*/

$handle = fopen($stage.'NSRLMfg.txt', "r");
if(!$handle) {
	echo "couldn't load file NSRLMfg.txt";
} else {
	$mfg_array = array();
	while (!feof($handle)) {
		$line = fgetcsv($handle);
		$mfg_array[$line[0]] = $line[1];
	}
}

$handle = fopen($stage.'NSRLOS.txt', "r");
if(!$handle) {
	echo "couldn't load file NSRLOS.txt";
} else {
	$os_array = array();
	while (!feof($handle)) {
		$line = fgetcsv($handle);
		$os_array[$line[0]] = array($line[1], $line[2], $line[3]);
	}
}

$handle = fopen($stage.'NSRLProd.txt', "r");
if(!$handle) {
	echo "couldn't load file NSRLProd.txt";
} else {
	$prod_array = array();
	while (!feof($handle)) {
		$line = fgetcsv($handle);
		$prod_array[$line[0]] = array($line[1], $line[2], $line[3], $line[4], $line[5], $line[6]);
	}
}

//print_r($prod_array);
//
// at this point all of the additional data about the hashes has been loaded into arrays.
// we are going to load each line and make a directory, or put the file with info into
// an exsisting directory

$handle = fopen($stage.'NSRLFile.txt', "r");
if(!$handle) {
	echo "couldn't load file NSRLFile.txt";
} else {
	$dir_count = 0;
	$file_count = 0;
	while (!feof($handle)) {
		$line = fgetcsv($handle);
		$md5_split = str_split($line[1]);
		$first_md5_3 = $md5_split[0].$md5_split[1].$md5_split[2];
		//echo $data_store.$first_md5_3."\n";
		
		// build writeline by pulling data from other arrays
		$write_line = $line[0].','.$line[1].','.$line[2].','.$line[3].','.$line[4];
		
		foreach ($prod_array[$line[5]] as $key => $value){
			if(!$key == 3 || 4) {
				$write_line .= ','.$value;
			}
		}
		
		foreach ($os_array[$line[6]] as $key => $value){
			if(!$key == 3) {
				$write_line .= ','.$value;
			}
		}
		
		echo $write_line."\n";
		
		if(is_dir($data_store.$first_md5_3)) {
			//local_put_md5_info_in_file($write_line, $data_store.$first_md5_3);
			$file_count++;
		} else {
			mkdir($data_store.$first_md5_3, 0700);
			$dir_count++;
			//local_put_md5_info_in_file($write_line, $data_store.$first_md5_3);
			$file_count++;
		}
		/*
		// echo file counts to the screen
		$r = fmod($file_count, 10000);
		if($r == 0) {
			echo "Dir count: ".$dir_count."\n";
			echo "File count: ".$file_count."\n";
		}*/
	}
}

function local_array_str_lower($line) {
	foreach ($line as $key => $value) {
		strtolower($value);
	}
	
	return $line;
}

function local_put_md5_info_in_file ($write_line, $data_store_first_md5_3) {
	$fp = fopen($data_store_first_md5_3.'/'.$line[1], 'a');
	fwrite($fp, $write_line);
	fclose($fp);
}

?>